package com.jams.goodox.activity.serve.enterprise;

import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hangzhou.jin.customview.LoadView;
import com.jams.goodox.BaseActivity;
import com.jams.goodox.R;
import com.jams.goodox.adapter.CustomFragmentPagerAdapter;
import com.jams.goodox.entity.resp.PolicyDetailResp;
import com.jams.goodox.net.RetrofitCallbackListener;
import com.jams.goodox.net.services.EnterpriseService;
import com.jams.goodox.utils.permission.PhoneUtils;
import com.jams.goodox.utils.StringUtils;
import com.jams.goodox.utils.StyleUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;
import retrofit2.Call;


/**
 * 企业政策详情
 */
public class EnterprisePolicyDetailActivity extends BaseActivity {
    @Bind(R.id.tabs_policy)
    TabLayout tabsPolicy;
    @Bind(R.id.mPager)
    ViewPager mPager;
    @Bind(R.id.rl_hotline)
    RelativeLayout rlHotline;
    @Bind(R.id.tv_tittle)
    TextView tvTittle;
    @Bind(R.id.tv_read_number)
    TextView tvReadNumber;
    @Bind(R.id.read)
    TextView read;
    @Bind(R.id.tv_time)
    TextView tvTime;
    @Bind(R.id.ll_top)
    LinearLayout llTop;
    @Bind(R.id.hotline)
    TextView hotLine;
    @Bind(R.id.rl_content)
    RelativeLayout rlContent;
    @Bind(R.id.lv_load)
    LoadView emptyView;

    private FragmentPagerAdapter adapter;
    private List<String> tittleList = new ArrayList<>();
    private List<Fragment> contentList = new ArrayList<>();
    /**
     * 传入的id
     */
    private String id;
    /**
     * 咨询电话
     */
    private String phone;

    public static final String POLICY_ID = "POLICY_ID";


    @Override
    protected int getLayoutId() {
        return R.layout.activity_enterprise_policy_detail;
    }

    @Override
    protected void init() {
        StyleUtil.titleBackKey(this, "详情");
        id = StringUtils.newString(getIntent().getStringExtra(POLICY_ID));
        initView();
        getPolicyDetailsInfo();
    }

    /**
     * 获取详情页数据
     */
    private void getPolicyDetailsInfo() {
        showLoading();
        enqueue(createCallService(EnterpriseService.class).getPolicyDetailsInfo(id), new RetrofitCallbackListener<PolicyDetailResp>() {
            @Override
            public void onSuccess(Call<PolicyDetailResp> call, PolicyDetailResp result) {
                misLoading();
                refreshData(result);
            }

            @Override
            public void onError(String msg) {
                misLoading();
                emptyView.setVisibility(View.VISIBLE);
            }
        });
    }

    /**
     * 从网络获得并刷新数据
     */
    private void refreshData(final PolicyDetailResp result) {
        try {
            llTop.setVisibility(View.VISIBLE);
            rlContent.setVisibility(View.VISIBLE);
            rlHotline.setVisibility(View.VISIBLE);
            emptyView.setVisibility(View.GONE);
            tvTittle.setText(result.data.title);
            tvTime.setText(result.data.createTime);
            tvReadNumber.setText(result.data.readNum + "");
            phone = StringUtils.newString(result.data.telephone);
            contentList.add(EnterprisePolicyDetailFragment.newInstance(StringUtils.newString(result.data.policyFile)));
            contentList.add(EnterprisePolicyDetailFragment.newInstance(StringUtils.newString(result.data.admission)));
            tittleList.add("政策文件");
            tittleList.add("申请条件");
            if (adapter != null) {
                adapter.notifyDataSetChanged();
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    @OnClick(R.id.rl_hotline)
    public void onViewClicked() {
        /*Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + phone));
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);*/
        PhoneUtils phoneUtils = new PhoneUtils(mContext);
        phoneUtils.makeCall(StringUtils.newString(""),phone);
    }

    /**
     * adapter初始化
     */
    private void initView() {
        if (tittleList != null && contentList != null) {
            adapter = new CustomFragmentPagerAdapter(getSupportFragmentManager(), tittleList, contentList);
            mPager.setAdapter(adapter);
            tabsPolicy.setupWithViewPager(mPager);
            emptyView.loadCompleteNoData(R.mipmap.holder_no_common,R.string.no_content);
        }
    }

}
